<template>
	<view class="category-container">
		<!-- 自定义导航栏 -->
		<!-- <view class="header-bag" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="nav-content">
				<image 
					src="/static/img/icon-Get-into@3.png" 
					class="back-btn" 
					@click="goBack"
				></image>
				<view class="header-center">
					<text class="header-title">团购分类</text>
				</view>
			</view>
		</view> -->

		<!-- 分类列表 -->
		<scroll-view 
			class="scroll-container" 
			scroll-y 
			refresher-enabled 
			:refresher-triggered="refreshing"
			@refresherrefresh="onRefresh"
		>
			<view class="wrapper">
				<!-- 加载状态 -->
				<view v-if="loading" class="loading-state">
					<text class="loading-text">加载中...</text>
				</view>

				<!-- 空状态 -->
				<view v-else-if="categoryList.length === 0" class="empty-state">
					<text class="empty-text">暂无分类数据</text>
				</view>

				<!-- 分类列表 -->
				<view v-else>
					<view 
						v-for="(category, index) in categoryList" 
						:key="category.id"
						class="category-section"
					>
						<!-- 分类标题 -->
						<view class="category-title font13">
							<view class="title-left">
								<image 
									:src="getImageUrl(category.file_path)" 
									class="category-icon"
								></image>
								<text class="category-name">{{ category.name }}</text>
							</view>
							<view class="title-right" @click="viewAllCategory(category)">
								<text class="all-text">全部</text>
								<image 
									src="/static/img/o2o/jt_youhui@2x.png" 
									class="arrow-icon"
								></image>
							</view>
						</view>

						<!-- 子分类列表 -->
						<view class="subcategory-list font13">
							<view 
								v-for="(subCategory, subIndex) in category.child" 
								:key="subCategory.id"
								class="subcategory-item"
								@click="selectSubCategory(category, subCategory)"
							>
								<text class="subcategory-name">{{ subCategory.name }}</text>
							</view>
						</view>

						<!-- 分隔线 -->
						<view class="divider"></view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import api from '../../../api/index.js'

export default {
	data() {
		return {
			// 系统信息
			statusBarHeight: 0,
			
			// 页面数据
			categoryList: [],
			loading: false,
			refreshing: false,
			
			// API配置
			baseUrl: 'https://api.youpin91.com'
		}
	},
	
	onLoad() {
		this.initPage()
	},
	
	onShow() {
		// 页面显示时可以刷新数据
		if (this.categoryList.length === 0) {
			this.loadCategories()
		}
	},
	
	methods: {
		// 初始化页面
		initPage() {
			// 获取系统信息
			const systemInfo = uni.getSystemInfoSync()
			this.statusBarHeight = systemInfo.statusBarHeight || 0
			
			// 加载分类数据
			this.loadCategories()
		},
		
		// 返回上一页
		goBack() {
			uni.navigateBack({
				delta: 1
			})
		},
		
		// 下拉刷新
		async onRefresh() {
			this.refreshing = true
			try {
				await this.loadCategories()
			} finally {
				this.refreshing = false
			}
		},
		
		// 加载分类数据
		async loadCategories() {
			if (this.loading) return
			
			this.loading = true
			
			try {
				const res = await api.o2oApi.getCateList()
				
				console.log('分类数据:', res)
				
				if (res.r === 0 && res.data && res.data.list) {
					this.categoryList = res.data.list
				} else {
					console.error('加载分类失败:', res.msg)
					uni.showToast({
						title: res.msg || '加载失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('加载分类失败:', error)
				uni.showToast({
					title: '网络错误，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		},
		
		// 查看全部分类
		viewAllCategory(category) {
			uni.navigateTo({
				url: `/pages/o2o/shenghuofw?cate_id=${category.id}&category_name=${encodeURIComponent(category.name)}`
			})
		},
		
		// 选择子分类
		selectSubCategory(parentCategory, subCategory) {
			// 跳转到商家列表页面，传递顶级分类ID
			uni.navigateTo({
				url: `/pages/o2o/shenghuofw?cate_id=${parentCategory.id}&sub_cate_id=${subCategory.id}&category_name=${encodeURIComponent(subCategory.name)}`
			})
		},
		
		// 获取完整图片URL
		getImageUrl(path) {
			if (!path) return '/static/img/default-category.png'
			if (path.startsWith('http')) return path
			return `${this.baseUrl}/${path}`
		}
	}
}
</script>

<style scoped>
/* 基础样式 */
.category-container {
	background-color: #eee;
	min-height: 100vh;
}

/* 头部导航 */
.header-bag {
	background: #ff1e00;
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 999;
}

.nav-content {
	height: 88rpx;
	display: flex;
	align-items: center;
	position: relative;
	padding: 0 30rpx;
}

.back-btn {
	width: 40rpx;
	height: 40rpx;
	position: absolute;
	left: 30rpx;
	z-index: 1;
}

.header-center {
	flex: 1;
	display: flex;
	justify-content: center;
	align-items: center;
}

.header-title {
	font-size: 36rpx;
	color: #fff;
	font-weight: 500;
}

/* 滚动容器 */
.scroll-container {
	height: 100vh;
	padding-top: calc(var(--status-bar-height, 0px) + 88rpx);
}

.wrapper {
	padding: 20rpx;
}

/* 加载和空状态 */
.loading-state,
.empty-state {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 200rpx 0;
}

.loading-text,
.empty-text {
	font-size: 28rpx;
	color: #999;
}

/* 分类区块 */
.category-section {
	margin-bottom: 20rpx;
}

.category-title {
	height: 60rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	/* background-color: #fff; */
	padding: 0 30rpx;
	/* border-radius: 20rpx 20rpx 0 0; */
}

.title-left {
	display: flex;
	align-items: center;
}

.category-icon {
	width: 44rpx;
	height: 44rpx;
	margin-right: 15rpx;
}

.category-name {
	font-size: 30rpx;
	color: #212121;
	font-weight: 500;
}

.title-right {
	display: flex;
	align-items: center;
	padding: 20rpx;
	margin-top: 20rpx;
}

.all-text {
	font-size: 28rpx;
	color: #656565;
	margin-right: 10rpx;
}

.arrow-icon {
	width: 24rpx;
	height: 24rpx;
	margin: 0 30rpx;
}

/* 子分类列表 */
.subcategory-list {
	/* background-color: #fff; */
	padding: 20rpx;
	/* border-radius: 0 0 20rpx 20rpx; */
	display: flex;
	flex-wrap: wrap;
	gap: 10rpx;
}

.subcategory-item {
	box-sizing: border-box;
	width: calc(25% - 8.5rpx);
	height: 70rpx;
	line-height: 70rpx;
	text-align: center;
	background-color: #f8f9fa;
	border-radius: 20rpx;
	transition: all 0.3s ease;
	border: 2rpx solid transparent;
}

.subcategory-item:active {
	background-color: #e9ecef;
	border-color: #ff1e00;
	transform: scale(0.98);
}

.subcategory-name {
	font-size: 26rpx;
	color: #6e6e6e;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	padding: 0 10rpx;
	display: block;
}

/* 分隔线 */
.divider {
	width: 100%;
	height: 20rpx;
	background: #eeeeee;
	margin-bottom: 20rpx;
}

/* 字体大小类 */
.font13 {
	font-size: 26rpx;
}

/* 清除浮动 */
.clear {
	margin-bottom: 20rpx;
}

.clear::after {
	content: "";
	display: table;
	clear: both;
}

/* 响应式适配 */
@media screen and (max-width: 750rpx) {
	.subcategory-item {
		width: calc(33.333% - 6.67rpx);
	}
}

@media screen and (max-width: 600rpx) {
	.subcategory-item {
		width: calc(50% - 5rpx);
	}
}
</style>